package karaoke.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Cliente;
import karaoke.modelo.JsonResult;
import karaoke.modelo.Reserva;
import karaoke.modelo.Sala;
import karaoke.negocio.GestionReserva;
import karaoke.negocio.GestionSalas;

import com.google.gson.Gson;

/**
 * Servlet implementation class operacionReservaServlet
 */
@WebServlet("/admin/S_operacionReservaServlet")
public class S_operacionReservaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public S_operacionReservaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Gson gson = new Gson();
		JsonResult result = new JsonResult();
		
		// si recibe idreserva entonces modificamos, caso contrario es una insercion
		String idreserva = request.getParameter("txtidreserva");
		
		int idSala = Integer.parseInt(request.getParameter("winselsala"));
		String fecha = request.getParameter("txtfecha");
		String hora = request.getParameter("txthora");
		int nrohoras = Integer.parseInt(request.getParameter("nrohoras"));
		String descrip = request.getParameter("txtdescripcion");
		String message = "";
		int tipoMsg = JsonResult.WARNING;
		
		HttpSession sesion = request.getSession();
		Cliente c = (Cliente)sesion.getAttribute("CLIENTE");
		
		try {
			
			if(nrohoras>0){
				GestionSalas gestSala = new GestionSalas();
				Sala sala = gestSala.obtener(idSala);
				GestionReserva negocioR = new GestionReserva();
				//fecha = DateFormat.getFechaHoraSave(fecha, hora);
				Reserva reservaC = null;
				if (idreserva==null){
					reservaC = negocioR.registrarReservaCliente(sala, fecha, hora, nrohoras, descrip, c);
				} else {
					//deberia de modificar reserva
				}
				
				if (reservaC.getIdreserva()>0) {
					tipoMsg = JsonResult.SUCCESS;
					message = "["+reservaC.getIdreserva()+"] Reserva se grabo satisfactoriamente";
				} else {
					tipoMsg = JsonResult.ERROR;
					message = "No se pudo registrar";
				}
			} else {
				tipoMsg = JsonResult.WARNING;
				message = "Nro.Horas debe ser mayor que cero";
			}
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			message = e.getMessage();
		}
		result.addMenssage(tipoMsg, message);
		
		PrintWriter out = response.getWriter();
		out.println(gson.toJson(result));
	}

}
